/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package plures.transaction;

import br.ufu.sdg12014.base.replycoordinator.Operacao;
import java.io.Serializable;
import java.util.ArrayList;

/**
 *
 * @author Guilherme
 */
public class Command implements Serializable
{
    private Commands            signature;
    private ArrayList<Object>   args;

    public Command(Commands command, ArrayList<Object> args)
    {
        this.signature = command;
        this.args = args;
    }
    
    public Command(Operacao op)
    {
        switch(op.operacao.toLowerCase())
        {
            case "create": this.signature = Commands.CREATE;
                break;
            case "read": this.signature = Commands.READ;
                break;
            case "update": this.signature = Commands.UPDATE;
                break;
            case "delete": this.signature = Commands.DELETE;
                break;
        }
        this.args = new ArrayList<>();
        this.args.add(Integer.parseInt(op.argumentos.get(0)));
        if(op.argumentos.size() == 2)
        {
            this.args.add(op.argumentos.get(1));
        }
    }
    
    public Operacao toOperacao()
    {
        Operacao op = new Operacao();
        switch(this.signature)
        {
            case CREATE: op.operacao = "create";
                break;
            case READ: op.operacao = "read";
                break;
            case UPDATE: op.operacao = "update";
                break;
            case DELETE: op.operacao = "delete";
                break;
        }
        ArrayList<String> arguments = new ArrayList<>();
        for(Object arg : this.args)
        {
            arguments.add(String.valueOf(arg));
        }
        op.argumentos = arguments;
        return op;
    }

    public Commands getSignature()
    {
        return signature;
    }
    
    public int getKey()
    {
        return (Integer) this.args.get(0);
    }
    
    public String getValue()
    {
        return (String) this.args.get(1);
    }

    public ArrayList<Object> getArgs()
    {
        return this.args;
    }
    
    
}
